package com.xts.weinfo.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.MenuAdapter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.xts.weinfo.R;
import com.xts.weinfo.base.Constants;
import com.xts.weinfo.bean.DataBean;
import com.xts.weinfo.bean.ItInfoTabBean;
import com.xts.weinfo.ui.adapter.RlvInterestAdapter;
import com.xts.weinfo.widget.SimleCallback;

import java.util.ArrayList;

public class InterestActivity extends AppCompatActivity {

    private ArrayList<DataBean> mList;
    private RecyclerView mRlv;
    private RlvInterestAdapter mAdapter;

    /**
     * @param context
     * @param list    只能是ArrayList,不能是List
     */
    public static void startAct(Activity context, ArrayList<DataBean> list) {
        Intent intent = new Intent(context, InterestActivity.class);
        intent.putExtra(Constants.DATA, list);
        context.startActivityForResult(intent,100);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest);
        initView();

    }

    /**
     * 全量刷新
     * 我们平常使用RecyclerView适配器刷新时一般使用:notifyDataSetChanged();
     *
     *
     *notifyItemInserted(position);//通知插入了一条数据
     * 2
     * notifyItemMoved(fromPosition,toPosition);//通知两条数据换位置
     * 3
     * notifyItemRemoved(position);//通知移除了一条数据
     * 4
     * notifyItemChanged(position);//通知某个数据发生了改变
     * 5
     * notifyItemRangeInserted(positionStart,itemCount);//通知从某个位置开始插入数据,共插入了几条
     * 6
     * notifyItemRangeChanged(positionStart,itemCount);//通知某个位置开始数据发生了改变,共有几个改变了
     * 7
     * notifyItemRangeRemoved(positionStart,itemCount);//通知从某个位置开始移除itemCount个数据
     *
     *
     */
    private void initView() {
        mList = (ArrayList<DataBean>) getIntent().getSerializableExtra(Constants.DATA);
        mRlv = (RecyclerView) findViewById(R.id.rlv);

        mRlv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new RlvInterestAdapter(mList);
        mRlv.setAdapter(mAdapter);

        //侧滑删除和拖拽移动
        SimleCallback simleCallback = new SimleCallback(mAdapter);
        //不允许左滑删除
        simleCallback.setSwipeEnabled(false);
        ItemTouchHelper helper = new ItemTouchHelper(simleCallback);
        //关联ItemTouchHelper和RecyclerView
        helper.attachToRecyclerView(mRlv);
    }

    /**
     * 返回键点击事件的回调
     */
    @Override
    public void onBackPressed() {
        //
        Intent intent = new Intent();
        intent.putExtra(Constants.DATA, mAdapter.mList);
        setResult(RESULT_OK,intent);
        //finish();
        super.onBackPressed();
    }
}
